package com.qzsoft.zhjd.machine.modules.machineinfo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.machine.modules.machineinfo.dto.MachineInfoDriverDto;
import com.qzsoft.zhjd.machine.modules.machineinfo.entity.MachineInfoDriverEntity;
import com.qzsoft.zhjd.machine.modules.machineinfo.service.MachineInfoDriverService;

/**
 * @ClassName MachineInfoDriverController
 * @Description 机械设备驾驶员控制器
 * @author linhuawei
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/api/machineInfoDriver")
public class MachineInfoDriverController {

    @Autowired
    private MachineInfoDriverService machineInfoDriverService;

    /**
     * @Title list
     * @Description 获取机械设备驾驶员列表
     * @param machineId 机械设备主键
     * @return 驾驶员列表
     */
    @GetMapping("/page/{machineId}")
    public Result<IPage<MachineInfoDriverDto>> page(@PathVariable("machineId") String machineId,
            @RequestParam Map<String, Object> params) {
        // 拼接查询条件
        if (null == params) {
            params = new HashMap<String, Object>();
        }
        params.put("machineId", machineId);

        // 查询数据列表
        IPage<MachineInfoDriverDto> data = machineInfoDriverService.pageDto(params);
        return ResultUtils.ok(data);
    }

    /**
     * @Title: find
     * @Description: 获取机械设备驾驶员
     * @param id 机械设备驾驶员主键
     * @return 机械设备驾驶员
     */
    @GetMapping("/find/{id}")
    public Result<MachineInfoDriverEntity> find(@PathVariable("id") String id) {
        MachineInfoDriverEntity entity = machineInfoDriverService.getById(id);
        if (null == entity) {
            return ResultUtils.error(BizCode.DATA_NOT_FOUND);
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title: save
     * @Description: 添加机械设备驾驶员
     * @param entity 机械设备驾驶员
     * @return 添加结果
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody @Validated(AddGroup.class) MachineInfoDriverEntity entity) {
        machineInfoDriverService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: update
     * @Description: 修改机械设备驾驶员
     * @param entity 机械设备驾驶员
     * @return 修改结果
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated(UpdateGroup.class) MachineInfoDriverEntity entity) {
        machineInfoDriverService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title: delete
     * @Description: 删除机械设备驾驶员
     * @param id 机械设备驾驶员主键
     * @return 删除结果
     */
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        machineInfoDriverService.removeById(id);
        return ResultUtils.ok();
    }

}
